#include <stm32f10x.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ParseString.h>
#include <Command.h>
#include <Config.h>

/*
 * return 1: got file extension
 * 		  0: can't get file extension
 */
unsigned int ParseFileExtension(const char *filename, char *extension) {
	unsigned int pos = strlen(filename) - 1;
	while (1) {
		if (filename[pos] == '.') {
			break;
		}
		if (pos == 0) {
			return 0;
		}
		pos--;
	}
	substring(pos + 1, strlen(filename), filename, extension);
	if (strlen(extension) > 4) {
		return 0;
	}
	return 1;
}

/*
 * return CMDRES
 */
CMDRES DetectFileType(char *filename) {
	char *FileText = ".C.TXT.S.";
	char *FileMp3 = ".MP3.";
	char *FileWAV = ".WAV.";
	char ExtTmp[8];
	char fileExt[6];
	if (ParseFileExtension(filename, fileExt) == 0) {
		return file_dir_cmd;
	}
	sprintf(ExtTmp, ".%s.", fileExt);
	strupr(ExtTmp);
	if (FindInString(FileText, ExtTmp) != -1) {
		return file_open_text_cmd;
	}
	if (FindInString(FileMp3, ExtTmp) != -1) {
		return file_open_mp3_cmd;
	}
	if (FindInString(FileWAV, ExtTmp) != -1) {
		return file_open_wav_cmd;
	}
	return file_dir_cmd;
}

unsigned int CheckTrueFilename(const char *filename) {
	char forbidden[] = "\\/*?\":<>|";
	unsigned int pos_fob = 0;
	unsigned int pos = 0;
	for (pos = 0; pos < strlen(filename); pos++) {
		for (pos_fob = 0; pos_fob < strlen(forbidden); pos_fob++) {
			if (filename[pos] == forbidden[pos_fob]) {
				return 0;
			}
		}
	}
	return 1;
}

/**************************************************************************
 * parse a raw command string
 *
 * return
 **************************************************************************/
CMDRES ParseCMD_filemode(char *str_recv) {
	if (FindInString(str_recv, "CMD_file_") != -1) {
		while (StringCut(str_recv, "\r") != 0)
			;
		while (StringCut(str_recv, "\n") != 0)
			;
		if (FindInString(str_recv, "_dir") != -1) {
			strcpy(str_recv, "0:");
			return file_dir_cmd;
		} else if (FindInString(str_recv, "_open_") != -1) {
			StringCut(str_recv, "CMD_file_open_");
			return DetectFileType(str_recv);
		} else if (FindInString(str_recv, "_stop_wav") != 1) {
			return file_stop_wav_cmd;
		} else {
			return error_cmd;
		}
	} else {
		return error_cmd;
	}
}

/*
 *
 */
CMDRES ParseCMD_dacmode(char *str_recv, unsigned int *valA, unsigned int *valB) {
	char str_tmp[15];
	int temp = 0;
	if (FindInString(str_recv, "CMD_dac_") != -1) {
		while (StringCut(str_recv, "\r") != 0)
			;
		while (StringCut(str_recv, "\n") != 0)
			;
		if (FindInString(str_recv, "_const_") != -1) {
			StringCut(str_recv, "CMD_dac_const_");
			*valA = (unsigned int) atoi(str_recv);
			return dac_const_cmd;
		} else if (FindInString(str_recv, "_sine_") != -1) {
			StringCut(str_recv, "CMD_dac_sine_");
			*valA = (unsigned int) atoi(str_recv);
			return dac_sine_cmd;
		} else if (FindInString(str_recv, "_square_") != -1) {
			StringCut(str_recv, "CMD_dac_square_");
			*valA = (unsigned int) atoi(str_recv);
			return dac_square_cmd;
		} else if (FindInString(str_recv, "_triangular_") != -1) {
			StringCut(str_recv, "CMD_dac_triangular_");
			*valA = (unsigned int) atoi(str_recv);
			return dac_triangular_cmd;
		} else if (FindInString(str_recv, "_squarecustom_") != -1) {
			StringCut(str_recv, "CMD_dac_squarecustom_");
			StringTokenc(str_recv, '_', str_tmp);
			*valA = (unsigned int) atoi(str_recv);
			*valB = (unsigned int) atoi(str_tmp);
			return dac_squarecustom_cmd;
		} else {
			return error_cmd;
		}
	} else {
		return error_cmd;
	}
}
/*
 *
 */
CMDRES ParseCMD_adcmode(char *str_recv, unsigned int *value) {
	if (FindInString(str_recv, "CMD_adc_") != -1) {
		while (StringCut(str_recv, "\r") != 0)
			;
		while (StringCut(str_recv, "\n") != 0)
			;
		if (FindInString(str_recv, "_read") != -1) {
			return adc_read_cmd;
		} else if (FindInString(str_recv, "_set_alarm_min_") != -1) {
			StringCut(str_recv, "CMD_adc_set_alarm_min_");
			*value = (unsigned int) atoi(str_recv);
			return adc_set_alarm_min_cmd;
		} else if (FindInString(str_recv, "_set_alarm_max_") != -1) {
			StringCut(str_recv, "CMD_adc_set_alarm_max_");
			*value = (unsigned int) atoi(str_recv);
			return adc_set_alarm_max_cmd;
		} else if (FindInString(str_recv, "_get_alarm_min") != -1) {
			return adc_get_alarm_min_cmd;
		} else if (FindInString(str_recv, "_get_alarm_max") != -1) {
			return adc_get_alarm_max_cmd;
		} else if (FindInString(str_recv, "_act_alarm_") != -1) {
			StringCut(str_recv, "CMD_adc_act_alarm_");
			*value = (unsigned int) atoi(str_recv);
			return adc_act_alarm_cmd;
		} else {
			return error_cmd;
		}
	} else {
		return error_cmd;
	}
}

/*
 *
 */
CMDRES ParseCMD_ledmode(char *str_recv, unsigned int *delay) {
	if (FindInString(str_recv, "CMD_led_") != -1) {
		while (StringCut(str_recv, "\r") != 0)
			;
		while (StringCut(str_recv, "\n") != 0)
			;
		if (FindInString(str_recv, "_shr_") != -1) {
			StringCut(str_recv, "CMD_led_shr_");
			*delay = (unsigned int) atoi(str_recv);
			return led_shr_cmd;
		} else if (FindInString(str_recv, "_shl_") != -1) {
			StringCut(str_recv, "CMD_led_shl_");
			*delay = (unsigned int) atoi(str_recv);
			return led_shl_cmd;
		} else if (FindInString(str_recv, "_inc_lr_") != -1) {
			StringCut(str_recv, "CMD_led_inc_lr_");
			*delay = (unsigned int) atoi(str_recv);
			return led_inc_lr_cmd;
		} else if (FindInString(str_recv, "_inc_rl_") != -1) {
			StringCut(str_recv, "CMD_led_inc_rl_");
			*delay = (unsigned int) atoi(str_recv);
			return led_inc_rl_cmd;
		} else if (FindInString(str_recv, "_dec_lr_") != -1) {
			StringCut(str_recv, "CMD_led_dec_lr_");
			*delay = (unsigned int) atoi(str_recv);
			return led_dec_lr_cmd;
		} else if (FindInString(str_recv, "_dec_rl_") != -1) {
			StringCut(str_recv, "CMD_led_dec_rl_");
			*delay = (unsigned int) atoi(str_recv);
			return led_dec_rl_cmd;
		} else if (FindInString(str_recv, "_incdec_lr_") != -1) {
			StringCut(str_recv, "CMD_led_incdec_lr_");
			*delay = (unsigned int) atoi(str_recv);
			return led_incdec_lr_cmd;
		} else if (FindInString(str_recv, "_incdec_rl_") != -1) {
			StringCut(str_recv, "CMD_led_incdec_rl_");
			*delay = (unsigned int) atoi(str_recv);
			return led_incdec_rl_cmd;
		} else {
			return error_cmd;
		}
	} else {
		return error_cmd;
	}
}

